package com.tyqx.hashchainbiz.service.impl;

import java.util.ArrayList;

import com.tyqx.hashchainbean.module.TrackUser;
import com.tyqx.hashchaindao.mapper.TrackUserMapper;
import hashchainutil.exception.HashchainException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class JwtUserDetailsService implements UserDetailsService {


    @Resource
    private TrackUserMapper trackUserMapper;

    @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            TrackUser trackUser=trackUserMapper.getUserByName(username);
            if (trackUser!=null) {
                return new User(username, trackUser.getPassword(),
                        new ArrayList<>());
            } else {
                throw new UsernameNotFoundException("User not found with username: " + username);
            }
        } catch (HashchainException e) {
            e.printStackTrace();
        }
        return null;
    }
}
